package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import com.mission.schedule.R;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.NoteTypeBean;
import com.mission.schedule.my160920.widget.EditTextC;
import com.mission.schedule.sinaadd.BaseDialog;
import com.mission.schedule.utils.SharedPrefUtil;

/**
 * 清单类别对话框
 * Created by liwei on 2017/4/18.
 */

public class NoteTypeNameEditDialog extends Dialog {


    CallBack callBack;

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    EditTextC editTextC;
    InputMethodManager imm;
    View view;
    String id;
    String name;
    int type = 0;

    public NoteTypeNameEditDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager, final String id, final String name, int type) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;
        this.name = name;
        this.id = id;
        this.type = type;

        setCanceledOnTouchOutside(true);

        view = LayoutInflater.from(context).inflate(R.layout.dialog_notetype_name_edit, null);
        setContentView(view);
        imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        editTextC = (EditTextC) view.findViewById(R.id.notetypename_et);
        editTextC.setText(name);
        editTextC.setSelection(name.length());
        editTextC.setEdittextCoutomListener(new EditTextC.EdittextCoutomListener() {
            @Override
            public void enter(int index, String text) {
                save();
            }

            @Override
            public void delete(int index, String text) {

            }

            @Override
            public void textchange(int index, boolean hastext) {

            }
        });
        imm.showSoftInput(editTextC, 0);
    }

    void save() {
        if (editTextC.getText().toString().isEmpty()) {
            Toast.makeText(context, "分类名称不能为空", Toast.LENGTH_SHORT).show();
        } else {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            if (type == 1) {
                App.getDBcApplication().updateNoteTypeName(id, editTextC.getText().toString());
            } else {
                App.getDBcApplication().updateCalenderTypeName(id, editTextC.getText().toString());
            }
            callBack.updateTypeName(editTextC.getText().toString());
            dismiss();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        /* 触摸外部弹窗 */
        if (isOutOfBounds(getContext(), event)) {
            save();
            return false;
        }
        return super.onTouchEvent(event);
    }

    private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void updateTypeName(String name);
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
}
